package com.hyx.nettystudy.discard;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;

/**
 * @ClassName DiscardServerHandler
 * @Description TODO
 * @Author 黄乙轩
 * @Date 2021/11/15 11:24
 * @Version 1.0
 **/

public class DiscardServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("DiscardServerHandler");
        ctx.fireChannelRead(msg);
//        ByteBuf in = (ByteBuf) msg;
//        try {
//            while (in.isReadable()){
//                System.out.print((char) in.readByte());
//                System.out.flush();
//                ctx.write("over!");
//                ctx.flush();
//                ctx.close();
//            }
//        }finally {
//            ReferenceCountUtil.release(msg);
//        }
//        ctx.write(msg);
//        ctx.flush();
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
